﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Configuration;
using System.Configuration;

public partial class InviteFriends : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["Redirect"] = Request.Url.PathAndQuery;
        if (Session["TaiKhoan"] != null)
        {
            if (IsTaiKhoan() == null)
                Response.Redirect("Login.aspx");
        }else
            Response.Redirect("Login.aspx");
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        
        string id = "";
        if (Request.QueryString["ID"] != null)
            id = Request.QueryString["ID"].ToString();
        SmtpSection cfg = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
        string checkError = SendEmailProviders.CheckError(SendEmailProviders.RoleSendEmail.User,
                "businessem07.somee.com",
                txtFullName.Text,
                txtEmailFrom.Text,
                txtEmailTo.Text,
                "Tài liệu hay",
                "http://businessem07.somee.com/Product.aspx?ID="+id).ToString();
        if (checkError == "")
        {
            NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();

            var query = (from sp in db.TaiLieus
                         where sp.ProductID.Equals(id)
                         select sp).Single();

               string msg= SendEmailProviders.SendInviteFriends(
                SendEmailProviders.RoleSendEmail.User,
                query,
                "businessem07.somee.com",
                txtFullName.Text,
                txtEmailFrom.Text,
                txtEmailTo.Text,
                "Tài liệu - "+txtFullName.Text,                
                Server.MapPath("MailInviteFriends.htm")).ToString();
               NotificationMessage.InnerHtml = NotificationHelper.SetNotification(NotificationHelper.Notification.success, msg);
        }
        else
        {
            NotificationMessage.InnerHtml = NotificationHelper.SetNotification(NotificationHelper.Notification.error,string.Format("<div class=\"MessageError\">{0}<br/>{1}</div>", checkError, "Mã bảo vệ không trùng khớp.Vui lòng nhập lại"));
        }
    }

    private TaiKhoan IsTaiKhoan()
    {
        TaiKhoan tk = null;
        try
        {
            tk = (TaiKhoan)Session["TaiKhoan"];
        }
        catch
        {

        }
        return tk;
    }

    protected void Refresh()
    {
        txtFullName.Text = "";
        txtEmailTo.Text = "";        
        txtEmailFrom.Text = "";
        NotificationMessage.InnerHtml = "";
    }
}